import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 注解方式开发Servlet
 * 1. @WebServlet 是一个注解
 * 2. @WebServlet 源码
 * @Target({ElementType.TYPE})
 * @Retention(RetentionPolicy.RUNTIME)
 * @Documented
 * public @interface WebServlet {
 *     String name() default "";
 *
 *     String[] value() default {};
 *
 *     String[] urlPatterns() default {};
 *
 *     int loadOnStartup() default -1;
 *
 *     WebInitParam[] initParams() default {};
 *
 *     boolean asyncSupported() default false;
 *
 *     String smallIcon() default "";
 *
 *     String largeIcon() default "";
 *
 *     String description() default "";
 *
 *     String displayName() default "";
 * }
 * 3. urlPatterns 对应 web.xml 的<url-pattern></url-pattern>
 * 4. {"/ok1", "/ok2"} 可以给okServlet配置多个url-pattern
 * 5. 相当于这个 @WebServlet(urlPatterns = {"/ok1", "/ok2"})代替了web.xml配置
 * 6. 浏览器可以这样访问OkServlet http://localhost:8080/ok1 或  http://localhost:8080/ok2
 * 7. 可以根据 @interface WebServlet配置各种属性(除了servlet-class)
 * 8. url-patterns匹配规则
 *    精确匹配：urlPatterns = {"/ok1", "/ok2"} ok1 ok2
 *    目录匹配：urlPatterns = {"/ok/*"}    ok/aaa ok/bbb/ccc
 *    扩展名匹配：urlPatterns = {"*.action"}    .../servlet/test.action
 *    任意匹配：urlPatterns = {"/","/*"} 会匹配所有请求，避免这样使用
 */
@WebServlet(urlPatterns = {"/ok1", "/ok2"}, loadOnStartup = 1)
@SuppressWarnings({"all"})
public class S5_OkServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("注解方式 S5_OkServlet init()");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("注解方式 S5_OkServlet  doPost()");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("注解方式 S5_OkServlet  doGet()");
    }
}
